#!/usr/bin/env python
# -*- coding: cp936 -*-
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys
import os
ProjectPath = os.path.dirname(os.getcwd())
sys.path.insert(0, ProjectPath)
sys.path.insert(0, os.path.join(ProjectPath, 'common'))
sys.path.insert(0, os.path.join(ProjectPath, 'database'))

import logging
logging.getLogger().setLevel(logging.INFO)

import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

import databasefunction
import commonfunction




class CAdminPage(commonfunction.CBasePage):
    def get(self):
        TemplateValues = self.GetDefaultTemplateValue()
        AdminConfig = commonfunction.GetAdminConfig();
        TemplateValues.update( { 'AdminConfig':AdminConfig, } )
	
        TemplateFilePath = self.GetRightLanguageHtmlFilePath('admin')

	self.response.out.write(template.render(TemplateFilePath, TemplateValues))

    def post(self):
        IsAdmin = commonfunction.UserIsAdmin()
        if IsAdmin:
            AdminConfig = commonfunction.GetAdminConfig();
            AdminConfig.IntegralPreBlog = int(self.request.get('IntegralPreBlog', '2'))
            AdminConfig.IntegralPreCommentOn = int(self.request.get('IntegralPreCommentOn', '1'))
            AdminConfig.IntegralPreJoin = int(self.request.get('IntegralPreJoin', '10'))
            AdminConfig.IntegralRegister = int(self.request.get('IntegralRegister', '10'))
        
            AdminConfig.MicroblogNeedAuditing = int(self.request.get('MicroblogNeedAuditing', '0'))
            AdminConfig.AnonymousCanCommentON = int(self.request.get('AnonymousCanCommentON', '0'))

#            logging.error("--------------AdminConfig %s" % AdminConfig.__dict__)

            commonfunction.SetAdminConfig(AdminConfig)
        
        self.redirect('/admin')
      

    def head(self):
        pass


def main():
    application = webapp.WSGIApplication([('/admin', CAdminPage),],
                                             debug=True)
    
    wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
    main()


